package com.hugesoft.jxgs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.base.BaseActivity;
import com.hugesoft.jxgs.utils.CheckInputUtil;
import com.hugesoft.logic.managers.LogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.utils.Util;

public class RegActivity extends BaseActivity implements OnClickListener{
	private static final String TAG = "RegActivity";
	private EditText mPoliceName,mPoliceNo,mPwd,mConfirmPwd;
	private Button mNextStepBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initUI();
	}
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			dismissDialog();
			switch (msg.what) {
			case Constans.REG_FAILED:
				if (msg.obj != null) {
					Util.showToast(RegActivity.this,
							msg.obj.toString());
				} else {
					Util.showToast(RegActivity.this, msg.obj != null?msg.obj.toString():"注册失败");
				}
				break;
			case Constans.REG_SUCCESS:
				Util.showToast(RegActivity.this, "注册成功");
				showWaitDialog("欢迎你使用本系统.正为你自动登录中，请稍候");
//				LogicManager.loginManager().Login(LogicManager.registManager().getRegPhoneNo(), 
//						LogicManager.registManager().getRegPwd(), LOGINTYPE.TypeTel, mHandler);
				LogicManager.policeManager().login(mPoliceNo.getText().toString(), mPwd.getText().toString(),
						mHandler);
				break;
			case Constans.LOGIN_SUCCESS:
				//登录成功，摘取该用户的信息
//				LogicManager.friendManager().getVerboseFriendInfo(LogicManager.accountManager().getCurrentAccoutSystemId(),mHandler);
				Util.showToast(RegActivity.this, "登陆成功");
				Intent intent = new Intent(RegActivity.this,SelectTypeActivity.class);
				startActivity(intent);
				RegActivity.this.finish();
				break;
			case Constans.LOGIN_FAILED:
				Toast.makeText(RegActivity.this,
						msg.obj == null ? "登陆失败" : msg.obj.toString(),
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};
	private void initUI(){
		setContentView(R.layout.activityreg);
		((TextView)findViewById(R.id.titleTextView)).setText("注册");
		((ImageView)findViewById(R.id.backImg)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RegActivity.this.finish();
			}
		});
//		mCheckBox = (CheckBox)findViewById(R.id.agree_checkbox);
		mPoliceName = (EditText)findViewById(R.id.username_edittext);
		mPoliceNo = (EditText)findViewById(R.id.id_policeno_edittext);
		mPwd = (EditText)findViewById(R.id.pwd_edittext);
		mConfirmPwd = (EditText)findViewById(R.id.confirm_pwd_edittext);
		
		
		
		mNextStepBtn = (Button)findViewById(R.id.nextstep);
		mNextStepBtn.setOnClickListener(this);
//		mRbtnMale = (RadioButton)findViewById(R.id.rbtnmale);
//		mRbtnFemale = (RadioButton)findViewById(R.id.rbtnfemale);
	
	}
	
	@Override
	public void onClick(View v) {
		String stringid;
		switch (v.getId()) {
		
		case R.id.nextstep:
//			if (mAccount.getText().toString().trim().equals("")||mIdnumber.getText().toString().trim().equals("")||mPwd.getText().toString().trim().equals("")||mConfirmPwd.getText().toString().trim().equals("")) {
//				Util.showToast(AdultRegActivity.this, "帐户密码身份证信息不能为空");
//				return;
//			}
			if (mPoliceName.getText().toString().trim().equals("")) {
				Util.showToast(RegActivity.this, "真实姓名不能为空");
				return;
			}
//			if (mIdnumber.getText().toString().trim().length()!=18) {
//				Util.showToast(AdultRegActivity.this, "身份证号必须为18位");
//				return;
//			}
			
//			if (!mCheckBox.isChecked()) {
//				Util.showToast(RegActivity.this, "请勾选同意使用条款");
//				return;
//			}
//			if (mEmail.getText().toString().trim().equals("")) {
//				Util.showToast(AdultRegActivity.this, "请填写邮箱地址");
//				return;
//			}
			if (!mPwd.getText().toString().equals(mConfirmPwd.getText().toString())) {
				Util.showToast(RegActivity.this, "两次输入的密码不一致");
				return;
			}
			if (mPwd.getText().toString().trim().length()<6||mConfirmPwd.getText().toString().trim().length()<6) {
				Util.showToast(RegActivity.this, "密码最少为6位");
				return;
			}
			LogicManager.policeManager().reg(mPoliceName.getText().toString(), mPoliceNo.getText().toString(), mPwd.getText().toString(), mHandler);
//			startActivity(new Intent(AdultRegActivity.this, AdultRegPhoneCheckActivity.class));
			showWaitDialog("注册中,请稍候...");
			break;
		default:
			break;
		}
	}
	
	private boolean checkpolice() {
		boolean ret = true;
		
		if (mPoliceName.getText() == null || !CheckInputUtil.isnameverified(mPoliceName.getText().toString())) {
			Util.showToast(RegActivity.this, "名字错了哦，亲");
			ret = false;
			return ret;
		}
		
		if (mPoliceNo.getText() == null || !CheckInputUtil.ispolicenoverified((mPoliceNo.getText().toString()))){
			Util.showToast(RegActivity.this, "警号错了哦，亲");
			ret = false;
			return ret;
		}
		
		if (mPwd.getText() == null || !CheckInputUtil.ispolicepasswordverified((mPwd.getText().toString()))){
			Util.showToast(RegActivity.this, "密码错了哦，亲");
			ret = false;
			return ret;
		}
		
		if (mConfirmPwd.getText() == null || !CheckInputUtil.ispolicepasswordverified((mConfirmPwd.getText().toString()))){
			Util.showToast(RegActivity.this, "确认密码错了哦，亲");
			ret = false;
			return ret;
		}
		
		if (!mConfirmPwd.getText().toString().equalsIgnoreCase(mPwd.getText().toString())){
			Util.showToast(RegActivity.this, "两次输入密码不一致哦，亲");
			ret = false;
			return ret;
		}
		
		return ret;
	}
}
